package greentycoon.gui;

import greentycoon.batiment.Batiment;
import greentycoon.batiment.Habitation;
import greentycoon.batiment.Route;
import greentycoon.batiment.centrale.CentraleCharbon;
import greentycoon.batiment.centrale.CentraleEolienne;
import greentycoon.batiment.centrale.CentraleNucleaire;
import greentycoon.batiment.centrale.CentralePhotovoltaique;
import greentycoon.batiment.decharge.CentreEnfouissementTechnique;
import greentycoon.batiment.decharge.Decharge;
import greentycoon.batiment.decharge.DechargeACielOuvert;
import greentycoon.main.VariablesGlobales;
import greentycoon.ville.Conteneur;
import greentycoon.ville.Ville;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GUI extends JFrame implements ConteneurListener,EnvironmentListener {
	
	public static final String gameName = "Green Tycoon";
	
	List<Batiment> environmentList = new ArrayList();
	private Conteneur[][] map;
	private boolean panel1Usable = true;
	private static final long serialVersionUID = 1L;
	private int worldSizeX, worldSizeY;
	private float ratioXY;
	private int numberOfcaseX;
	private int numberOfcaseY;
	private int imageSizeX;
	private int imageSizeY;


	private MainPanel panel;

	private MainPanel backPanel;
	
	/*Labels et icones*/
	private static JLabel houseLabel;
	private static JLabel houseWithoutEnergyLabel;
	private static JLabel emptyHouseLabel;
	private static JLabel emptyHouseWithoutEnergyLabel;
	private static JLabel grassLabel;
	private static JLabel roadLabel;
	private static JLabel centraleEolienneLabel;
	private static JLabel centraleCharbonLabel;
	private static JLabel centraleNucleaireLabel;
	private static JLabel centralePhotovoltaiqueLabel;
	private static JLabel dechargeACielOuvertLabel;
	private static JLabel centreEnfouissementtechniqueLabel;
	private static ImageIcon grassPicture;
	private static ImageIcon housePicture;	
	private static ImageIcon houseWithoutEnergyPicture;
	private static ImageIcon emptyHousePicture;
	private static ImageIcon emptyHouseWithoutEnergyPicture;
	private static ImageIcon roadPicture;
	private static ImageIcon centraleCharbonPicture;
	private static ImageIcon centraleEoliennePicture;
	private static ImageIcon centraleNucleairePicture;
	private static ImageIcon centralePhotovoltaiquePicture;
	private static ImageIcon dechargeACielOuvertPicture;
	private static ImageIcon centreEnfouissementTechniquePicture;

	
	
	private static MenuCreation menuCreation;
	private static Ville ville;
	
	public GUI(int worldSizeX, int worldSizeY, MenuCreation menuCreation, Ville _ville) {
		
		ville=_ville;
		/*VariablesGlobales.fenetreSelectionCentrale = new FenetreSelectionCentrale();
		VariablesGlobales.fenetreSelectionCentrale.setVisible(false);
		VariablesGlobales.fenetreRechercheCentrale = new FenetreRechercheCentrale();
		VariablesGlobales.fenetreRechercheCentrale.setVisible(false);
		VariablesGlobales.fenetreSelectionDecharge = new FenetreSelectionDecharge();
		VariablesGlobales.fenetreSelectionDecharge.setVisible(false);*/
		GUI.menuCreation = menuCreation;


		this.map = ville.getMap();
		this.worldSizeX=worldSizeX;
		this.worldSizeY=worldSizeY;
		setTitle(gameName);
		setSize(worldSizeX, worldSizeY);
		/*setExtendedState(MAXIMIZED_BOTH);
		setUndecorated(true); */
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		
		ratioXY = (float) worldSizeX/worldSizeY;
		numberOfcaseY = 30;
		numberOfcaseX = (int) (numberOfcaseY*ratioXY);
		imageSizeX = worldSizeX/(int) (numberOfcaseY*ratioXY);
		imageSizeY = worldSizeY/numberOfcaseY;
		
		VariablesGlobales.TAILLE_CASE_X = imageSizeX;
		VariablesGlobales.TAILLE_CASE_Y = imageSizeY;
		VariablesGlobales.TAILLE_MAP_X = numberOfcaseX;
		VariablesGlobales.TAILLE_MAP_Y = numberOfcaseY;
		
		/*On ajoutes les différent menus au panel*/
		panel = new MainPanel(ville);
        panel.setLayout(null);
        add(panel);
        panel.add(menuCreation);
        /*panel.add(VariablesGlobales.fenetreRechercheCentrale);
        panel.add(VariablesGlobales.fenetreSelectionCentrale);
        panel.add(VariablesGlobales.fenetreSelectionDecharge);*/

		
		
		//chargement des images
		housePicture = rescaleImage(new File("src/greentycoon/gui/maison.png"), imageSizeY, imageSizeX);
		houseWithoutEnergyPicture = rescaleImage(new File("src/greentycoon/gui/maisonSansCourant.png"), imageSizeY, imageSizeX);
		emptyHousePicture = rescaleImage(new File("src/greentycoon/gui/maisonVide.png"), imageSizeY, imageSizeX);
		emptyHouseWithoutEnergyPicture = rescaleImage(new File("src/greentycoon/gui/maisonVideSansCourant.png"), imageSizeY, imageSizeX);
		grassPicture = rescaleImage(new File("src/greentycoon/gui/grass.png"), imageSizeY, imageSizeX);
		roadPicture = rescaleImage(new File("src/greentycoon/gui/route.png"), imageSizeY, imageSizeX);
		centraleCharbonPicture = rescaleImage(new File("src/greentycoon/gui/centraleCharbon.png"), imageSizeY*CentraleCharbon.sizeY, imageSizeX*CentraleCharbon.sizeX);
		centraleEoliennePicture = rescaleImage(new File("src/greentycoon/gui/eolienne.png"), imageSizeY*CentraleEolienne.sizeY, imageSizeX*CentraleEolienne.sizeX);
		centraleNucleairePicture = rescaleImage(new File("src/greentycoon/gui/centraleNucleaire.png"), imageSizeY*CentraleNucleaire.sizeY, imageSizeX*CentraleNucleaire.sizeX);
		centralePhotovoltaiquePicture = rescaleImage(new File("src/greentycoon/gui/centralePhotovoltaique.png"), imageSizeY*CentralePhotovoltaique.sizeY, imageSizeX*CentralePhotovoltaique.sizeX);
		dechargeACielOuvertPicture = rescaleImage(new File("src/greentycoon/gui/dechargeACielOuvert.png"), imageSizeY*DechargeACielOuvert.sizeY, imageSizeX*DechargeACielOuvert.sizeX);
		centreEnfouissementTechniquePicture = rescaleImage(new File("src/greentycoon/gui/centreEnfouissementTechnique.png"), imageSizeY*CentreEnfouissementTechnique.sizeY, imageSizeX*CentreEnfouissementTechnique.sizeX);
		
		setIconImage((rescaleImage(new File("src/greentycoon/gui/greenFlouz.png"), imageSizeY, imageSizeX)).getImage());
		reafficher();	
		
		
	}
	
	public void reafficher() { //cette fonction refresh l'interface graphique
		backPanel = new MainPanel(ville);
		backPanel.setLayout(null);
		backPanel.add(menuCreation);
		/*backPanel.add(VariablesGlobales.fenetreRechercheCentrale);
		backPanel.add(VariablesGlobales.fenetreSelectionCentrale);
		backPanel.add(VariablesGlobales.fenetreSelectionDecharge);*/
		
		for(int j=0;j<numberOfcaseY;j++) {
			for(int i=0;i<numberOfcaseX;i++) {
				if(map[i][j].getBat()instanceof Habitation) {
					if(map[i][j].getBat().isSousAlimentaion())
					{
						if(((Habitation) map[i][j].getBat()).isInhabited())
						{
							houseLabel = new JLabel(housePicture);
							houseLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX,imageSizeY);
							backPanel.add(houseLabel);	
						}else{
							emptyHouseLabel = new JLabel(emptyHousePicture);
							emptyHouseLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX,imageSizeY);
							backPanel.add(emptyHouseLabel);	
						}
					}else{
						
						if(((Habitation) map[i][j].getBat()).isInhabited())
						{
							houseWithoutEnergyLabel = new JLabel(houseWithoutEnergyPicture);
							houseWithoutEnergyLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX,imageSizeY);
							backPanel.add(houseWithoutEnergyLabel);
						}else{
							emptyHouseWithoutEnergyLabel = new JLabel(emptyHouseWithoutEnergyPicture);
							emptyHouseWithoutEnergyLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX,imageSizeY);
							backPanel.add(emptyHouseWithoutEnergyLabel);
						}
					}
					

				}else if (map[i][j].getBat()instanceof Route)
				{
					roadLabel = new JLabel(roadPicture);
					roadLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX,imageSizeY);
					backPanel.add(roadLabel);	

					

				}else if(map[i][j].getBat()instanceof CentraleCharbon){
					centraleCharbonLabel = new JLabel(centraleCharbonPicture);
					centraleCharbonLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX*CentraleCharbon.sizeX,imageSizeY*CentraleCharbon.sizeY);
					backPanel.add(centraleCharbonLabel);
					
					
				}else if(map[i][j].getBat()instanceof CentraleEolienne){
					centraleEolienneLabel = new JLabel(centraleEoliennePicture);
					centraleEolienneLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX*CentraleEolienne.sizeX,imageSizeY*CentraleEolienne.sizeY);
					backPanel.add(centraleEolienneLabel);
					
					
				}else if(map[i][j].getBat()instanceof CentraleNucleaire){
					centraleNucleaireLabel = new JLabel(centraleNucleairePicture);
					centraleNucleaireLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX*CentraleNucleaire.sizeX,imageSizeY*CentraleNucleaire.sizeY);
					backPanel.add(centraleNucleaireLabel);
					
					
				}else if(map[i][j].getBat()instanceof CentralePhotovoltaique){
					centralePhotovoltaiqueLabel = new JLabel(centralePhotovoltaiquePicture);
					centralePhotovoltaiqueLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX*CentralePhotovoltaique.sizeX,imageSizeY*CentralePhotovoltaique.sizeY);
					backPanel.add(centralePhotovoltaiqueLabel);
					
					
				}else if(map[i][j].getBat()instanceof DechargeACielOuvert){
					dechargeACielOuvertLabel = new JLabel(dechargeACielOuvertPicture);
					dechargeACielOuvertLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX*DechargeACielOuvert.sizeX,imageSizeY*DechargeACielOuvert.sizeY);
					dechargeACielOuvertLabel.setToolTipText("contenu : " + ((Decharge) (map[i][j].getBat())).getDechetStocke() + "/" + ((Decharge) (map[i][j].getBat())).getCapaciteTotal());
					backPanel.add(dechargeACielOuvertLabel);
					
					
				}else if(map[i][j].getBat()instanceof CentreEnfouissementTechnique){
					centreEnfouissementtechniqueLabel = new JLabel(centreEnfouissementTechniquePicture);
					centreEnfouissementtechniqueLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX*CentreEnfouissementTechnique.sizeX,imageSizeY*CentreEnfouissementTechnique.sizeY);
					centreEnfouissementtechniqueLabel.setToolTipText("contenu : " + ((Decharge) (map[i][j].getBat())).getDechetStocke() + "/" + ((Decharge) (map[i][j].getBat())).getCapaciteTotal());
					backPanel.add(centreEnfouissementtechniqueLabel);
					
					
				}else {
				
					grassLabel = new JLabel(grassPicture);
					grassLabel.setBounds(i*imageSizeX,j*imageSizeY,imageSizeX,imageSizeY);
					//backPanel.add(grassLabel);


				}
			}
		}
		remove(panel);
		add(backPanel);
		setVisible(true);
		backPanel.setVisible(true);
		panel=backPanel;
		
		
	}


	public static ImageIcon rescaleImage(File source,int maxHeight, int maxWidth)
	{
	    int newHeight = 0, newWidth = 0;        // Variables for the new height and width
	    int priorHeight = 0, priorWidth = 0;
	    BufferedImage image = null;
	    ImageIcon sizeImage;

	    try {
	            image = ImageIO.read(source);        // get the image
	    } catch (Exception e) {

	            e.printStackTrace();
	            System.out.println("Picture upload attempted & failed");
	    }

	    sizeImage = new ImageIcon(image);

	    if(sizeImage != null)
	    {
	        priorHeight = sizeImage.getIconHeight(); 
	        priorWidth = sizeImage.getIconWidth();
	    }

	    // Calculate the correct new height and width
	    if((float)priorHeight/(float)priorWidth > (float)maxHeight/(float)maxWidth)
	    {
	        newHeight = maxHeight;
	        newWidth = (int)(((float)priorWidth/(float)priorHeight)*(float)newHeight);
	    }
	    else 
	    {
	        newWidth = maxWidth;
	       newHeight = (int)(((float)priorHeight/(float)priorWidth)*(float)newWidth);
	    }


	    // Resize the image

	    // 1. Create a new Buffered Image and Graphic2D object
	    BufferedImage resizedImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB_PRE);
	    Graphics2D g2 = resizedImg.createGraphics();

	    // 2. Use the Graphic object to draw a new image to the image in the buffer
	    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    g2.drawImage(image, 0, 0, newWidth, newHeight, null);
	    g2.dispose();

	    // 3. Convert the buffered image into an ImageIcon for return
	    return (new ImageIcon(resizedImg));
	}

	@Override
	public void actionConteneurListener(int i, int j) {
		// TODO Auto-generated method stub
		reafficher();
		
	}

	@Override
	public void actionEnvironmentListener() {
		reafficher();
		
	}
	

}
